<?php
    /**
     *  
     */
    class Client extends BaseModel
    {
        protected $_id;
        protected $_userId;
        protected $_clientId;
        protected $_name;
        protected $_email;
        protected $_tel;
        protected $_phone;
        protected $_addressNumber;
        protected $_address;
        protected $_birthday;
        protected $_memo;
        protected $_updateDate;
        protected $_properties;

        /**
         *  讀取資料表而產生, 請視同cache, 任何情況下皆不可更動該產生的內容
         *  @return Array()
         */
        public static function getTableDefinition()
        {
        }

        /**
         *  經資料表傳至程式中時所對應的 method 資訊
         *  @return Array()
         */
        public static function getTableFields()
        {
            return Array(
                "id"                    => "getId",
                "user_id"               => "getUserId",
                "client_id"             => "getClientId",
                "name"                  => "getName",
                "email"                 => "getEmail",
                "tel"                   => "getTel",
                "phone"                 => "getPhone",
                "address_number"        => "getAddressNumber",
                "address"               => "getAddress",
                "birthday"              => "getBirthdayByDb",
                "memo"                  => "getMemo",
                "update_date"           => "getUpdateDateByDb",
                "properties"            => "getProperties"
            );
        }

        /**
         *  reset to default value model
         *  @return Array()
         */
        public function resetValue() {
            $this->__sleep();
            $this->setId            ( -1 );
            $this->setUserId        ( 0 );
            $this->setClientId      ( 0 );
            $this->setName          ( NULL );
            $this->setEmail         ( NULL );
            $this->setTel           ( NULL );
            $this->setPhone         ( NULL );
            $this->setAddressNumber ( NULL );
            $this->setAddress       ( NULL );
            $this->setBirthday      ( strtotime('1980-01-01') );
            $this->setMemo          ( NULL );
            $this->setUpdateDate    ( time() );
            $this->setProperties    ( Array() );
        }

        /**
         *  validate model data
         *  @return messages Array()
         */
        public function validate()
        {
            $validateInfo = parent::validate();

            if( !$this->getName() ) {
                $validateInfo['name']['message'] = '不能空值';
            }

            // email validate , email can empty
            if( $this->getEmail() ) {
                $result = filter_var( $this->getEmail(), FILTER_VALIDATE_EMAIL );
                if(!$result) {
                    $validateInfo['email']['message'] = 'Email格式不正確';
                }
            }

            return $validateInfo;
        }

        /**
         *  filter model data
         */
        public function filter()
        {
            $this->setUserId        ( (int) $this->getUserId() );
            $this->setClientId      ( (int) $this->getClientId() );
            $this->setName          ( trim(strip_tags( $this->getName()          )) );
            $this->setEmail         ( filter_var($this->getEmail(), FILTER_SANITIZE_EMAIL) );
            $this->setTel           ( trim(strip_tags( $this->getTel()           )) );
            $this->setPhone         ( trim(strip_tags( $this->getPhone()         )) );
            $this->setAddressNumber ( trim(strip_tags( $this->getAddressNumber() )) );
            $this->setAddress       ( trim(strip_tags( $this->getAddress()       )) );
            $this->setMemo          ( trim(strip_tags( $this->getMemo()          )) );
        }

        /* ------------------------------------------------------------------------------------------------------------------------
            basic method 
        ------------------------------------------------------------------------------------------------------------------------ */

        /**
         *  get UserId
         *  @return int
         */
        public function getUserId()
        {
            return $this->_userId;
        }
        /**
         *  set UserId
         */
        public function setUserId( $userId )
        {
            $this->_userId = (int) $userId;
        }

        /**
         *  get ClientId
         *  @return int
         */
        public function getClientId()
        {
            return $this->_clientId;
        }
        /**
         *  set ClientId
         */
        public function setClientId( $clientId )
        {
            $this->_clientId = (int) $clientId;
        }

        /**
         *  get Name
         *  @return string
         */
        public function getName()
        {
            return $this->_name;
        }
        /**
         *  set Name
         */
        public function setName( $name )
        {
            $this->_name = trim($name);
        }

        /**
         *  get Email
         *  @return string
         */
        public function getEmail()
        {
            return $this->_email;
        }
        /**
         *  set Email
         */
        public function setEmail( $email )
        {
            $this->_email = trim($email);
        }

        /**
         *  get Tel
         *  @return string
         */
        public function getTel()
        {
            return $this->_tel;
        }
        /**
         *  set Tel
         */
        public function setTel( $tel )
        {
            $this->_tel = trim($tel);
        }

        /**
         *  get Phone
         *  @return string
         */
        public function getPhone()
        {
            return $this->_phone;
        }
        /**
         *  set Phone
         */
        public function setPhone( $phone )
        {
            $this->_phone = trim($phone);
        }

        /**
         *  get AddressNumber
         *  @return string
         */
        public function getAddressNumber()
        {
            return $this->_addressNumber;
        }
        /**
         *  set AddressNumber
         */
        public function setAddressNumber( $addressNumber )
        {
            $this->_addressNumber = trim($addressNumber);
        }

        /**
         *  get Address
         *  @return string
         */
        public function getAddress()
        {
            return $this->_address;
        }
        /**
         *  set Address
         */
        public function setAddress( $address )
        {
            $this->_address = trim($address);
        }

        /**
         *  set birthday
         *  @param int , date int
         */
        function setBirthday($int) 
        {
            $this->_birthday = (int) $int;
        }
        /**
         *  get birthday
         *  @return int;
         */
        function getBirthday() 
        {
            if( !$this->_birthday ) {
                $this->_birthday = time();
            }
            return $this->_birthday;
        }
        /**
         *  get birthday by format 
         *  @param  format , date format string
         *  @return string
         */
        function getBirthdayByFormat( $format="Y-m-d" )
        {
            return date($format,$this->getBirthday());
        }
        /**
         *  convert object-value to database 
         *  @return string
         */
        function getBirthdayByDb()
        {
            // "date"
            return date("Y-m-d H:i:s",$this->getBirthday());
        }


        /**
         *  get Memo
         *  @return string
         */
        public function getMemo()
        {
            return $this->_memo;
        }
        /**
         *  set Memo
         */
        public function setMemo( $memo )
        {
            $this->_memo = trim($memo);
        }


        /* ------------------------------------------------------------------------------------------------------------------------
            extends
        ------------------------------------------------------------------------------------------------------------------------ */


    }

